<template>
  <a-card :bordered="false" class="card-area">
    <div class="table-page-search-wrapper">
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item label="院系码">
              <a-input v-model="queryParam.ssyxm" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="院系名称">
              <a-input v-model="queryParam.ssyxmc" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="档案接收详细地址">
              <a-input v-model="queryParam.dajsdz" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="档案接收人">
              <a-input v-model="queryParam.dajsr" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="8" :sm="12">
            <a-form-item label="联系电话">
              <a-input v-model="queryParam.lxdh" placeholder="" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="12">
            <span class="table-page-search-submitButtons">
              <a-button style="margin: 0 8px" type="primary" @click="$refs.table.refresh(true)" >查询</a-button>
              <a-button type="primary" @click="openAdd">添加</a-button>
              <a-button style="margin-left: 8px" type="primary" @click="batchDelete">删除</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
          <a-alert class="alert" v-if="isShow" :message="message" :type="type" show-icon />
          <a-col :md="12" :sm="12">
            <div class="uploaders">
              <a-button  type="primary" @click="exportExl" >导出</a-button>
              <a-upload name="file"
                        accept=".xlsx"
                        :multiple="false"
                        :action="importUrl"
                        :showUploadList="false"
                        :headers="{
                            Authorization: this.$store.state.account.token
                          }"
                        @change="handleImport">
                <a-button style="margin-left: 8px" type="primary" :loading="loading">导入</a-button>
              </a-upload>
              <a-dropdown style="margin-left: 8px">
                <a-button type="primary" style="margin-left: 8px"> 硕士调档函 <a-icon type="down" /> </a-button>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a-upload name="file"
                        accept=".pdf"
                        :multiple="false"
                        :action="uploadSsUrl"
                        :showUploadList="false"
                        :headers="{
                            Authorization: this.$store.state.account.token
                          }"
                        @change="handleImport2">
                      <!-- <a-button type="link">上传硕士调档函模板</a-button> -->
                    </a-upload>
                  </a-menu-item>
                  <a-menu-item>
                    <a-button type="link" @click="exportddh('2')">生成硕士调档函</a-button>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
              <a-dropdown style="margin-left: 8px">
                <a-button type="primary" style="margin-left: 8px"> 博士调档函 <a-icon type="down" /> </a-button>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a-upload name="file"
                        accept=".pdf"
                        :multiple="false"
                        :action="uploadBsUrl"
                        :showUploadList="false"
                        :headers="{
                            Authorization: this.$store.state.account.token
                          }"
                        @change="handleImport2">
                      <!-- <a-button type="link">上传博士调档函模板</a-button> -->
                    </a-upload>
                  </a-menu-item>
                  <a-menu-item>
                    <a-button type="link" @click="exportddh('3')">生成博士调档函</a-button>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
              
              
            </div>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection">

      <span slot="serial" slot-scope="text, record, index">
        {{ index + 1 }}
      </span>

      <span slot="sszymc" slot-scope="text">
        {{ text || '全部' }}
      </span>

      <a slot="operation" slot-scope="a,b" @click="openEdit(b)">修改</a>
    </s-table>

    <shift-edit
      ref="shiftEdit"
      :visible.sync="visible"
      :editId.sync="editId"
      @cancel="visible = false"
      @create="handleCreate"
    />

  </a-card>
</template>

<script>
  import {STable} from '~'
  import shiftEdit from './edit'

  export default {
    components: {
      STable,
      shiftEdit
    },
    data() {
      return {
        loading: false,
        // 高级搜索 展开/关闭
        advanced: true,
        visible: false,
        editId: null,
        // 查询参数
        queryParam: {},
        postParams: {
          sssfbz: true
        },
        importUrl: `${process.env.SERVICE_URL}base/dwddgzxx/impDdhdwxx`,
        uploadBsUrl: `${process.env.SERVICE_URL}base/dwddgzxx/impBsTpl`,
        uploadSsUrl: `${process.env.SERVICE_URL}base/dwddgzxx/impSsTpl`,
        // 表头
        columns: [
          {
            title: '序号',
            scopedSlots: {customRender: 'serial'}
          },
          {
            title: '院系码',
            dataIndex: 'ssyxm'
          },
          {
            title: '院系名称',
            dataIndex: 'ssyxmc'
          },
          {
            title: '专业',
            dataIndex: 'sszymc',
            scopedSlots: {customRender: 'sszymc'}
          },
          {
            title: '档案接收详细地址',
            dataIndex: 'dajsdz'
          },
          {
            title: '档案接收人',
            dataIndex: 'dajsr'
          },
          {
            title: '联系电话',
            dataIndex: 'lxdh'
          },
          {
            title: '操作',
            dataIndex: 'operation',
            scopedSlots: {customRender: 'operation'}
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          console.log('loadData.parameter', parameter)
          parameter.pageNum = parameter.pageNo
          let params = Object.assign(this.queryParam, parameter)
          return this.$api.base.shiftList(params).then(res => {
            return {
              data: res.data.rows,
              pageNo: parameter.pageNo,
              pageSize: parameter.pageSize,
              totalPage: Math.ceil(res.data.total / parameter.pageSize),
              totalCount: res.data.total
            }
          })
        },
        selectedRowKeys: [],
        selectedRows: [],

        // custom table alert & rowSelection
        options: {
          alert: {
            show: true, clear: () => {
              this.selectedRowKeys = []
            }
          },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange
          }
        },
        optionAlertShow: true, // 是否显示alert，已选中
        confirmLoading: false,
        selectMenu: {},
        type: 'success',
        message: '11111',
        isShow: false,
      }
    },
    created() {
      // this.initSelectMenu()
    },
    methods: {
      filterOption(value, op) {
        return op.componentOptions.children[0].text.indexOf(value) !== -1
      },
      handleCreate(additionParams) {
        const form = this.$refs.shiftEdit.form
        form.validateFields(async (err, values) => {
          if (err) {
            return
          }
          // 循环 日期参数，格式化
          this.$DateToString(values)
          this.confirmLoading = true
          let baseApi = this.editId ? 'shiftUpdate' : 'shiftAdd'
          values.ssyxm = values.ssyx.key
          if (!values.ssyxmc) {
            values.ssyxmc = values.ssyx.label.split(' ')[1] ? values.ssyx.label.split(' ')[1] : values.ssyx.label
          }
          if (values.sszy && values.sszy.key) {
            values.sszym = values.sszy.key
            values.sszymc = values.sszy.label.split(' ')[1] ? values.sszy.label.split(' ')[1] : values.sszy.label
          }
          delete values.ssyx
          delete values.sszy
          let res = await this.$api.base[baseApi](Object.assign({id: this.editId}, additionParams, values))
          this.$message.success(res.msg)
          this.$refs.table.refresh(false)
          this.confirmLoading = false
          form.resetFields()
          this.visible = false
        })
      },
      onSelectChange(selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      },
      batchDelete() {
        if (!this.selectedRowKeys.length) {
          this.$message.warning('请选择需要删除的调档信息')
          return
        }
        this.$confirm({
          title: '确定删除所选中的调档信息?',
          content: '当您点击确定按钮后，这些记录将会被彻底删除',
          centered: true,
          onOk: () => {
            let ids = []
            this.selectedRows.forEach(subject => {
              ids.push(subject.id)
            })
            this.$api.base.shiftDelete(ids.join(',')).then(() => {
              this.$message.success('删除成功')
              this.selectedRowKeys = []
              this.selectedRows = []
              this.$refs.table.refresh(false)
              this.$refs.table.clearSelected()
            })
          },
          onCancel: () => {
          }
        })
      },
      openAdd() {
        this.editId = null
        this.visible = true
        this.$refs.shiftEdit.form.resetFields()
        this.$refs.shiftEdit.stuData = []
      },
      openEdit(res) {
        this.visible = true
        this.$nextTick(() => {
          this.$api.base.shiftDetail(res.id).then(r => {
            // console.log('21121', r)
            if (r.data.dwddgzksList) {
              r.data.dwddgzksList.forEach(e => {
                e.id = e.ksxxId
              })
            }
            this.$refs.shiftEdit.stuData = r.data.dwddgzksList || []
          })
          this.$refs.shiftEdit.form.resetFields()
          this.$refs.shiftEdit.form.setFieldsValue(res)
          this.$refs.shiftEdit.form.setFieldsValue({
            ssyx: {
              label: res.ssyxmc,
              key: res.ssyxm
            },
            sszy: {
              label: res.sszymc,
              key: res.sszym
            }
          })
          this.$refs.shiftEdit.yxChange({
            label: res.ssyxmc,
            key: res.ssyxm
          }, 1)
          this.editId = res.id
        })
      },
      handleImport(info) {
        if (info.file.status === 'uploading') {
          this.loading = true
          return
        }
        if (info.file.status === 'done' && info.file.response.status == 1) {
          this.isShow = true
          this.message = `${info.file.name} 上传成功`
          this.type = 'success'
          // this.$message.success(`${info.file.name} 上传成功`);
        } else if (info.file.status === 'error') {
          this.isShow = true
          this.message = `${info.file.name} 上传失败`
          this.type = 'error'
          // this.$message.error(`${info.file.name} 上传失败`);
        }else if (info.file.response.status == 0){
          this.isShow = true
          this.message = `${info.file.response.msg}`
          this.type = 'error'
          // this.$message.error(`${info.file.response.msg}`);
        }
        setTimeout(()=> {
            this.isShow = false
          },3000)
          this.loading = false
      },
      handleImport2(info) {
        if (info.file.status === 'uploading') {
          // this.loading = true
          return
        }
        if (info.file.status === 'done' && info.file.response.status == 1) {
          this.isShow = true
          this.message = `${info.file.name} 上传成功`
          this.type = 'success'
          // this.$message.success(`${info.file.name} 上传成功`);
        } else if (info.file.status === 'error') {
          this.isShow = true
          this.message = `${info.file.name} 上传失败`
          this.type = 'error'
          // this.$message.error(`${info.file.name} 上传失败`);
        }else if (info.file.response.status == 0){
          this.isShow = true
          this.message = `${info.file.response.msg}`
          this.type = 'error'
          // this.$message.error(`${info.file.response.msg}`);
        }
        setTimeout(()=> {
            this.isShow = false
          },3000)
          // this.loading = false
      },
      exportExl(){
        this.$export('/base/dwddgzxx/export', {
          // ...params
        })
      },
      exportddh(num){
        let params = {kslb: num}
        console.log(params)
        this.$api.base.shiftDdh(params).then(r => {
          if(r.status == 1){
            this.$message.success(
              r.msg,
              2,
            );
          }else{
            this.$message.error(
              r.msg,
              2,
            );
          } 
        })
        // this.$export('/base/dwddgzxx/bindNlqmd', {
        //   ...params
        // })
      }
    }
  }
</script>
<style lang="less" scoped>
.uploaders{
  text-align: right;
  position: relative;
}
.alert{
  position: absolute;
  top: 80px;
  right: 20px;
}
.uploaders span{
  display: inline-block;
}
/deep/ .ant-upload-list-item-info{
  padding: 0 30px 0 4px;
}
</style>
